package com.medical.data.controller;

import com.aliyuncs.exceptions.ClientException;
import com.medical.data.service.PatientService;
import com.medical.data.service.SmsSendService;
import com.medical.data.utils.Result;
import com.medical.data.vo.GetPatientVo;
import com.medical.data.vo.OpenFollowUpVo;
import com.medical.data.vo.PatientVo;
import com.medical.data.vo.QueryPatientVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.ZoneId;
import java.util.Date;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/front/patient")
@Api(description = "患者管理")
@Slf4j
public class PatientController {
    @Autowired
    private PatientService patientService;

    @Autowired
    private SmsSendService smsSendService;

    @ApiOperation("新增患者病例")
    @PostMapping({"/addPatient"})
    public Result addPatient(@Validated @RequestBody PatientVo patientVo) {
        log.info("/front/patient/addPatient参数列表======>{}", patientVo);
        this.patientService.addPatient(patientVo);
        return Result.ok();
    }

    @ApiOperation("开启随访配置")
    @PostMapping({"/openFollowUp"})
    public Result openFollowUp(@Validated @RequestBody OpenFollowUpVo openFollowUpVo) {
        log.info("/front/patient/openFollowUp参数列表======>{}", openFollowUpVo);
        this.patientService.openFollowUp(openFollowUpVo);
        Date date = this.patientService.getFollowTime(openFollowUpVo.getPatientId());
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String formattedDate = sdf.format(date);
        return Result.ok().data("followUpTime", formattedDate);
    }

/*    @ApiOperation("创建患者病例")
    @PostMapping("/createPatient")
    public Result createPatient(@Validated @RequestBody PatientVo patientVo){
        log.info("/front/patient/createPatient参数列表======>{}",patientVo);
        patientService.createPatient(patientVo);
        return Result.ok();
    }*/

    @ApiOperation("查询患者病例")
    @PostMapping("/getPatient")
    public Result getPatient(@Validated @RequestBody GetPatientVo patientVo) {
        log.info("/front/patient/getPatient参数列表======>{}", patientVo);
        Map<String, Object> patientVos = patientService.getPatientPage(patientVo);
        return Result.ok().data("病例分页对象", patientVos);
    }

    @ApiOperation("查询该项目下患者病例")
    @PostMapping("/getPatientByProject")
    public Result getPatientByProject(@Validated @RequestBody QueryPatientVo queryPatientVo) {
        log.info("/front/patient/getPatientByProject参数列表======>{}", queryPatientVo);
        Map<String, Object> patientVos = patientService.getPatientByProject(queryPatientVo);
        return Result.ok().data("patients", patientVos);
    }

    @ApiOperation("短信提醒随访，传id，不是患者id")
    @PostMapping("/sendMessage/{patientId}/{text}")
    public Result testSendSms2(@PathVariable String patientId, @PathVariable String text) throws ClientException {
        log.info("/front/patient/sentMessage参数========>{}", patientId, text);
        smsSendService.sendSmsToPatient(patientId, text);
        return Result.ok();
    }

    @ApiOperation("删除患者，如果有随访一起删除")
    @PostMapping("/deletePatient/{patientIds}")
    public Result deletePatient(@PathVariable String patientIds) {
        log.info("/front/patient/sentMessage参数========>{}", patientIds);
        patientService.deleteById(patientIds);
        return Result.ok();
    }


}
